import { userInfo } from "@/apis/personal"

export default {
    namespaced: true,
    state: {
        // 登录态tocken
        token: '',
        // 用户信息
        userInfo: {}
    },
    getters: {
        isLogin: (state) => {
            return !!state.token || !!localStorage.getItem("pm_token")
        }
    },
    mutations: {
        // 设置token
        setToken(state, token) {
            // 保存在 localStorage
            localStorage.setItem("pm_token", token)
            state.token = token
        },
        clearToken(state) {
            state.token = ''
            localStorage.removeItem("pm_token")
        },
        // 设置用户信息
        setUserInfo(state, info) {
            state.userInfo = info || {}
        },
        clearUserInfo(state) {
            state.userInfo = {}
        }
    },
    actions: {
        refreshInfo({ commit }) {
            // 请求用户信息并赋值给vuex中的userInfo
            userInfo({}).then(res => {
                console.log('res==========', res);
                commit('setUserInfo', res.data)
            })
        }
    }
}